package com.wd.tw.controller;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wd.common.core.domain.HttpResponse;
import com.wd.tw.config.AppConfig;
import com.wd.tw.domain.dto.EventQuery;
import com.wd.tw.domain.dto.response.AlarmData;
import com.wd.tw.domain.dto.response.EventData;
import com.wd.tw.domain.dto.response.ImageListResponse;
import com.wd.tw.domain.dto.response.Pagination;
import com.wd.tw.domain.vo.EleRunState;
import com.wd.tw.service.*;
import com.wd.tw.utils.TimeUtils;
import com.wd.tw.domain.vo.PageData;
import com.wd.tw.domain.vo.Record;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

import static com.wd.tw.constants.EleConstant.ELE_CROWD_NUM;

/**
 * @className: EventController
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/

@RestController
public class EventController {

    @Resource
    AppConfig appConfig;

    @Resource
    ImageService imageService;

    @Resource
    BooleanCountService booleanCountService;

    @Resource
    AlarmService alarmService;

    @Resource
    EventService eventService;


    @Resource
    ReportService reportService;

    @RequestMapping("/event/list")
    public HttpResponse getEventData(EventQuery eventQuery) {

        ImageListResponse event = imageService.getImageList(eventQuery);
        Pagination pagination = event.getPagination();
        List<EventData> data = event.getData();

        PageData pageData = new PageData();
        pageData.setAuthorization(1);
        pageData.setResult_num(pagination.getPageSize());
        pageData.setTotal_num(pagination.getTotal());

        List<Record> recordList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            EventData eventData = data.get(i);
            Record record = new Record();
            record.setCamera_name(appConfig.getMap().get(eventData.getChannel_id()));
            record.setTrigger(TimeUtils.parseStr(eventData.getTimestamp()));
            record.setChannel(eventData.getChannel_id());
            record.setSnap_path("/image/edge?file=" + eventData.getTarget_image_path());
            record.setSnap_id(String.valueOf(eventData.getTarget_id()));
            record.setEvents_type("901");
            record.setFirst_alarm_type("901");
            recordList.add(record);
        }
        pageData.setRecord(recordList);

        HttpResponse httpResponse = HttpResponse.successData(pageData);
        httpResponse.setCode(0);
        return httpResponse;

    }


    @GetMapping("/alarm/list")
    public HttpResponse getAlarmList() {
      //  List<AlarmData> eventList = alarmService.getEventList();
        List<AlarmData> eventList=new ArrayList<>();
        HttpResponse httpResponse = HttpResponse.successData(eventList);
        httpResponse.setCode(0);
        return httpResponse;
    }

    @GetMapping("/person/count")
    public HttpResponse getPersonCount() {
        Map<Integer, EleRunState> curRunState = reportService.getCurRunState();
        HttpResponse httpResponse = HttpResponse.successData(curRunState);
        httpResponse.setCode(0);
        return httpResponse;
    }

    @GetMapping("/person/count1")
    public HttpResponse getPersonCount1() {
        HttpResponse httpResponse = HttpResponse.successData(booleanCountService.getRunTimeMap());
        httpResponse.setCode(0);
        return httpResponse;
    }

    @GetMapping("/event/push")
    public HttpResponse pushEvent()
    {
        eventService.edgeBoxEvent(3,"Edge_Box_01");
        return HttpResponse.success();
    }

}
